package com.xjh.basestudy.designmode.behaviorMode.behaviorMode9_Visitor.p2_batch;

import com.xjh.basestudy.designmode.behaviorMode.behaviorMode9_Visitor.p2_batch.Candy;
import com.xjh.basestudy.designmode.behaviorMode.behaviorMode9_Visitor.p2_batch.Fruit;
import com.xjh.basestudy.designmode.behaviorMode.behaviorMode9_Visitor.p2_batch.Visitor;
import com.xjh.basestudy.designmode.behaviorMode.behaviorMode9_Visitor.p2_batch.Wine;

import java.text.NumberFormat;
import java.time.LocalDate;

/**
 * @Author XuJianHua
 * @Date 2022/10/5 21:23
 * @Description 访问者实现类：根据不同商品类型计算商品价格
 * 折扣计价访问者DiscountVisitor实现了访问者接口Visitor，在第11行的糖果计价方法visit(Candy candy)中，我们对超过半年的糖果不予出售，否则按九折计价；因为酒不存在过期限制，
 * 所以我们在第25行的酒计价方法visit(Wine wine)中直接按其原价出售；最后在第33行的水果计价方法visit(Fruit fruit)中，我们规定水果的有效期为7天，如果只经过3天则按半价出售，并且在第44行按斤计价。
 *
 *

 */
public class DiscountVisitor implements Visitor {

    private LocalDate billDate;

    public DiscountVisitor(LocalDate billDate) {
        this.billDate = billDate;
        System.out.println("结算日期：" + billDate);
    }

    @Override
    public void visit(Candy candy) {
        System.out.println("=====糖果【" + candy.getName() + "】打折后价格=====");
        float rate = 0;
        long days = billDate.toEpochDay() - candy.getProducedDate().toEpochDay();
        if (days > 180) {
            System.out.println("超过半年的糖果，请勿食用！");
        } else {
            rate = 0.9f;
        }
        float discountPrice = candy.getPrice() * rate;
        System.out.println(NumberFormat.getCurrencyInstance().format(discountPrice));
    }

    @Override
    public void visit(Wine wine) {
        System.out.println("=====酒【" + wine.getName() + "】无折扣价格=====");
        System.out.println(
                NumberFormat.getCurrencyInstance().format(wine.getPrice())
        );
    }

    @Override
    public void visit(Fruit fruit) {
        System.out.println("=====水果【" + fruit.getName() + "】打折后价格=====");
        float rate = 0;
        long days = billDate.toEpochDay() - fruit.getProducedDate().toEpochDay();
        if (days > 7) {
            System.out.println("￥0.00元（超过7天的水果，请勿食用！）");
        } else if (days > 3) {
            rate = 0.5f;
        } else {
            rate = 1;
        }
        float discountPrice = fruit.getPrice() * fruit.getWeight() * rate;
        System.out.println(NumberFormat.getCurrencyInstance().format(discountPrice));
    }

}
